﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
Copyright 2011 Mirosław M. Mączka

This file is part of MobiLang.

    MobiLang is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
namespace MobiLang
{
    public class CETHX
    {
        struct RecordEXTH
        {
            public int Type;
            public string Name;
            public Type Hint;
            public RecordEXTH(int Type, string Name, Type Hint)
            {
                this.Type = Type;
                this.Name = Name;
                this.Hint = Hint;
            }
        };
        private readonly RecordEXTH[] re = {
                                    new RecordEXTH(1, "drm_server_id", null),
                                    new RecordEXTH(2, "drm_commerce_id", null),
                                    new RecordEXTH(3, "drm_ebookbase_book_id", null),
                                    new RecordEXTH(100, "author", typeof(string)),
                                    new RecordEXTH(101, "publisher", typeof(string)),
                                    new RecordEXTH(102, "imprint", typeof(string)),
                                    new RecordEXTH(103, "description", typeof(string)),
                                    new RecordEXTH(104, "isbn", typeof(string)),
                                    new RecordEXTH(105, "subject", typeof(string)),
                                    new RecordEXTH(106, "publishingdate", typeof(string)),
                                    new RecordEXTH(107, "review", typeof(string)),
                                    new RecordEXTH(108, "contributor", typeof(string)),
                                    new RecordEXTH(109, "rights", typeof(string)),
                                    new RecordEXTH(110, "subjectcode", typeof(string)),
                                    new RecordEXTH(111, "type", typeof(string)),
                                    new RecordEXTH(112, "source", typeof(string)),
                                    new RecordEXTH(113, "asin", typeof(string)),
                                    new RecordEXTH(114, "versionnumber", null),
                                    new RecordEXTH(115, "sample", typeof(int)),
                                    new RecordEXTH(116, "startreading", typeof(int)),
                                    new RecordEXTH(117, "adult", typeof(string)),
                                    new RecordEXTH(118, "retail price", typeof(string)),
                                    new RecordEXTH(119, "retail price currency", typeof(string)),
                                    new RecordEXTH(200, "Dictionary short name", typeof(string)),
                                    new RecordEXTH(201, "coveroffset", typeof(int)),
                                    new RecordEXTH(202, "thumboffset", typeof(int)),
                                    new RecordEXTH(203, "hasfakecover", typeof(int)),
                                    new RecordEXTH(204, "Creator Software", typeof(int)),
                                    new RecordEXTH(205, "Creator Major Version", typeof(int)),
                                    new RecordEXTH(206, "Creator Minor Version", typeof(int)),
                                    new RecordEXTH(207, "Creator Build Number", typeof(int)),
                                    new RecordEXTH(208, "watermark", null),
                                    new RecordEXTH(209, "tamper proof keys", null),
                                    new RecordEXTH(300, "fontsignature", null),
                                    new RecordEXTH(401, "clippinglimit", typeof(int)),
                                    new RecordEXTH(402, "publisherlimit", null),
                                    new RecordEXTH(403, "Unknown403", typeof(int)),
                                    new RecordEXTH(404, "ttsflag", typeof(bool)),
                                    new RecordEXTH(405, "Unknown", null),
                                    new RecordEXTH(406, "Unknown405", null),
                                    new RecordEXTH(407, "Unknown407", null),
                                    new RecordEXTH(450, "Unknown450", null),
                                    new RecordEXTH(451, "Unknown451", null),
                                    new RecordEXTH(452, "Unknown452", null),
                                    new RecordEXTH(453, "Unknown453", null),
                                    new RecordEXTH(501, "cdetype", typeof(string)),
                                    new RecordEXTH(502, "lastupdatetime", null),
                                    new RecordEXTH(503, "updatedtitle", typeof(string))
                                   };

        private string cnvString(byte [] bb)
        {
            int kount = bb.Length;
            for (int i = bb.Length - 1; i >= 0; i--)
            {
                if(0==bb[i])kount--;
                else break;
            }
            return System.Text.Encoding.UTF8.GetString(bb, 0, kount);
        }
        private string cnvInt(byte[] bb)
        {
            Array.Reverse(bb);
            return BitConverter.ToInt32(bb, 0).ToString();
        }
        private string cnvBool(byte[] bb)
        {
            return BitConverter.ToBoolean(bb, 0).ToString();
        }
        private string cnvNull(byte[] bb)
        {
            string s = string.Format("({0}):", bb.Length); ;
            //
            for(int i=0; i< bb.Length; i++)
            {
                s = s + bb[i].ToString();
                if (i != (bb.Length-1)) s += ",";
            }
            //
            return s;
        }
        public string Cnv(Type t, byte[] bb)
        {
            if (null != t)
            {
                if (t.Equals(typeof(string)))
                {
                    return cnvString(bb);
                }
                else if (t.Equals(typeof(int)))
                {
                    return cnvInt(bb);
                }
                else if (t.Equals(typeof(bool)))
                {
                    return cnvBool(bb);
                }
            }
            //
            return cnvNull(bb);
        }
        //
        //
        public string ToText(int EXTHRecordType, int EXTHRecordLength, byte[] bb)
        {
            string s = "";
            bool found = false;
            foreach (RecordEXTH _re in re)
            {
                if (_re.Type == EXTHRecordType)
                {
                    found = true;
                    s=string.Format("{0}: [{1}]", _re.Name, Cnv(_re.Hint, bb));
                    break;
                }
            }
            if (!found)
            {
                s = string.Format("Unknown:{0}: [{1}]", EXTHRecordType, cnvNull(bb));
            }
            return s;
        }
        //
        //
    }
}
